package com.bnz.service.impl;

import com.bnz.mapper.ClassesMapper;
import com.bnz.mapper.StudentMapper;
import com.bnz.pojo.Classes;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.pojo.StudentVo;
import com.bnz.service.StudentService;
import com.bnz.util.MybatisUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class StudentServiceImpl implements StudentService {

    /**
     * 1. 查询所有学生
     * @return
     */
    @Override
    public List<Student> findAll() {
        try(SqlSession sqlSession = MybatisUtil.getSqlSession()){
            //1.1 取得StudentMapper
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            //1.2 查询所有学生
            List<Student> students = mapper.findAll();
            //1.3 返回
            return students;
        }
    }

    /**
     * 2. 分页查询
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult<Student> findByPage(int page, int pagesize) {
        //2.1 开始分页
        PageHelper.startPage(page,pagesize);
        //2.2 开始查询
        try(SqlSession sqlSession = MybatisUtil.getSqlSession()){
            //2.3 取得StudentMapper
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            ClassesMapper classesMapper = sqlSession.getMapper(ClassesMapper.class);
            //2.4 查询所有学生
            List<Student> students = mapper.findAll();
            //2.5 将学生与班级名称绑定
            for (Student student : students) {
                Classes classes = classesMapper.findById(student.getCid());
                student.setCname(classes.getCname());
            }
            //2.6 转换为Page<Student>对象
            Page<Student> studentPage = (Page<Student>) students;
            //2.7 返回
            return new PageResult<>(studentPage.getTotal(),studentPage.getResult());
        }

    }

    /**
     * 3. 条件查询带分页
     * @param page
     * @param pagesize
     * @param vo
     * @return
     */
    @Override
    public PageResult<Student> search(int page, int pagesize, StudentVo vo) {
        //3.1 开始分页
        PageHelper.startPage(page,pagesize);
        //3.2 开始查询
        try(SqlSession session = MybatisUtil.getSqlSession()){
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
            //3.2.1 开始条件查询
            List<Student> students = mapper.search(vo);
            //3.2.2 将学生与班级名称绑定关系
            for (Student student : students) {
                Classes classes = classesMapper.findById(student.getCid());
                student.setCname(classes.getCname());
            }
            //3.3 转换为Page<Student>对象
            Page<Student> studentPage = (Page<Student>) students;
            //3.4 返回
            return new PageResult<>(studentPage.getTotal(),studentPage.getResult());
        }

    }
}
